package cn.edu.xmu.javaee.productdemoaop.mapper.flag;

import cn.edu.xmu.javaee.productdemoaop.mapper.flag.po.ProductAnyPo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface ProductAnyMapper {


    @Select("SELECT \n" +
            "    p.id,\n" +
            "    p.name,\n" +
            "    p.original_price,\n" +
            "    p.weight,\n" +
            "    p.barcode,\n" +
            "    p.unit,\n" +
            "    p.origin_place,\n" +
            "    os.price,\n" +
            "    os.quantity,\n" +
            "    os.max_quantity,\n" +
            "    (SELECT JSON_ARRAYAGG(\n" +
            "            JSON_OBJECT(\n" +
            "                'id', op.id,\n" +
            "                'name', op.name,\n" +
            "                'originalPrice', op.original_price,\n" +
            "                'weight', op.weight,\n" +
            "                'barcode', op.barcode,\n" +
            "                'unit', op.unit,\n" +
            "                'originPlace', op.origin_place\n" +
            "            )\n" +
            "        )\n" +
            "     FROM goods_product op\n" +
            "     WHERE op.goods_id = p.goods_id AND op.id <> p.id) AS otherProduct\n" +
            "FROM goods_product p\n" +
            "LEFT JOIN goods_onsale os ON os.product_id = p.id AND os.begin_time <= NOW() AND os.end_time > NOW()\n" +
            "WHERE p.name = #{name};")
    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.BIGINT),
            @Result(property = "name", column = "name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "originalPrice", column = "original_price", jdbcType = JdbcType.BIGINT),
            @Result(property = "weight", column = "weight", jdbcType = JdbcType.BIGINT),
            @Result(property = "barcode", column = "barcode", jdbcType = JdbcType.VARCHAR),
            @Result(property = "unit", column = "unit", jdbcType = JdbcType.VARCHAR),
            @Result(property = "originPlace", column = "origin_place", jdbcType = JdbcType.VARCHAR),
            @Result(property = "price", column = "price", jdbcType = JdbcType.BIGINT),
            @Result(property = "quantity", column = "quantity", jdbcType = JdbcType.INTEGER),
            @Result(property = "maxQuantity", column = "max_quantity", jdbcType = JdbcType.INTEGER),
            @Result(property = "otherProduct", column = "otherProduct", jdbcType = JdbcType.VARCHAR, javaType = String.class)
    })
    List<ProductAnyPo> selectProductByName(@Param("name") String name);
}
